package cn.edu.lingnan.util;

import java.sql.*;
import java.util.HashMap;

public class DBUtil {
    private static String driver=null;
    private static String url=null;
    private static String user=null;
    private static String password=null;
    private static Connection conn=null;
    private static String xmlFileName="database.conf.xml";
    private static String xsdFimeName="database.conf.xsd";

    static {
        String basePath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
        xmlFileName = basePath + xmlFileName;
        xsdFimeName = basePath + xsdFimeName;
        if (XmlValidator.validate(xmlFileName, xsdFimeName)) {
            //验证成功的情况下解析
            HashMap<String,String> hm=new HashMap<>();
            hm=XmlParser.parse(xmlFileName);
            driver= hm.get("driver");
            url= hm.get("url");
            user= hm.get("user");
            password= hm.get("password");
        }
        System.out.println(driver + "///" + url + "///" + user + "///" + password);
    }

    private static DBUtil dbUtil;
    private DBUtil(){//构造方法一般用来初始化
        try {
            Class.forName(driver);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    public static DBUtil getInstance(){
        if (dbUtil==null) {
            dbUtil=new DBUtil();
        }
        return dbUtil;
    }


    //对外提供一个获取数据库连接的方法
    public Connection getConnection(){

        try {
            conn = DriverManager.getConnection
                    (url,user,password);

        }  catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    public  void close(ResultSet rs,Statement stat,Connection conn){
        try {
            if (rs!=null)
                rs.close();
            if (stat!=null)
                stat.close();
            if (conn!=null)
                conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public  void close(ResultSet rs, PreparedStatement prep,Connection conn){
        try {
            if (rs!=null)
                rs.close();
            if (prep!=null)
                prep.close();
            if (conn!=null)
                conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public  void close(ResultSet rs1,PreparedStatement prep1,ResultSet rs2,PreparedStatement prep2,Connection conn){
        try {
            if (rs1!=null)
                rs1.close();
            if (prep1!=null)
                prep1.close();
            if (rs2!=null)
                rs2.close();
            if (prep2!=null)
                prep2.close();
            if (conn!=null)
                conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public void close(PreparedStatement prep, Connection conn) {
        try {
            if (prep!=null)
                prep.close();
            if (conn!=null)
                conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
